package com.zhiyou.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.zhiyou.entities.User;

/**
 * 连接数据库
 * 查询
 * @author admin
 *
 */
public class UserUtil {
	Connection conn=null;
	Statement st=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	//1.连接数据库的方法
	public void getConn() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			//局部变量
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/it40?useSSL=true&serverTimezone=GMT", "root", "123456");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//按条件查询
	
	public List<User> searchByCondition(String condition){
		StringUtil util=new StringUtil();
		List<User> list=new ArrayList<User>();
		getConn();
		String sql="select * from login where id=? or username like ?";
		try {
			ps=conn.prepareStatement(sql);
			if(util.isAbleFormat(condition)) {				
				ps.setInt(1, Integer.parseInt(condition));//有问题 abc-->int
			}else {
				ps.setInt(1, -1);
			}
			ps.setString(2, "%"+condition+"%");
			rs=ps.executeQuery();
			while(rs.next()) {
				User user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPwd(rs.getString(3));
				user.setRole(rs.getInt(4));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return list;
		
	}
	
	//获取权限
	public int getRoleByName(String username) {
		int role=-1;
		getConn();
		String sql="select role from login where username=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, username);
			rs=ps.executeQuery();
			if(rs.next()) {
				role=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return role;
	}
	
	
	//新增用户
	
	public int addUser(User user) {
		int i=0;
		getConn();
		String sql="insert into login (username,pwd) values(?,?)";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPwd());
			i=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {			
			closeAll();
		}
		return i;
	}
	
	//删除用户
	public int deleteUserById(int id) {
		int i=0;
		getConn();
		String sql="delete from login  where id=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			i=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {			
			closeAll();
		}
		return i;
	}
	
	//更新方法
	public int updateUserById(User user) {
		int i=0;
		getConn();
		String sql="update login set username=? , pwd=? where id=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPwd());
			ps.setInt(3, user.getId());
			i=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {			
			closeAll();
		}
		
		return i;
	}
	
	//根据id获取用户信息
	public User getUserById(int id) {
		User user=null;
		getConn();
		String sql="select * from login where id=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs=ps.executeQuery();
			if(rs.next()) {
				user=new User(rs.getInt(1), rs.getString(2), rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return user;
	}
	
	//获取所有用户信息   获取所有房屋信息
	public List<User> getAllUsers(String username,int role){
		List<User> list=new ArrayList<User>();
		getConn();
		String sql="select * from login where username=? or role < ?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setInt(2, role);
			rs=ps.executeQuery();
			while(rs.next()) {
				User user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPwd(rs.getString(3));
				user.setRole(rs.getInt(4));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return list;
		
	}
	
	public List<User> getAllUsers(){
		List<User> list=new ArrayList<User>();
		getConn();
		String sql="select * from login";
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				User user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPwd(rs.getString(3));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return list;
		
	}
	//登录
	public boolean login(String uname,String pwd) {
		getConn();
		String sql="select * from login where username='"+uname+"' and pwd='"+pwd+"'";//一下子查出了所有的用户信息
		System.out.println("sql: "+sql);
		try {
			st=conn.createStatement();
			//结果集要么只有一条记录，要么没有记录
			rs=st.executeQuery(sql);
			if(rs.next()) {//如果有值，说明数据库中肯定存在此用户
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return false;
	}
	
	//登录
	public boolean login2(String uname,String pwd) {
		getConn();
		//? 就是占位符
		String sql="select * from login where username=? and pwd=?";
		try {
			//创建ps对象，并且预编译sql语句
			ps=conn.prepareStatement(sql);
			//向sql语句中放值
			ps.setString(1, uname);//向第一个占位符中放值
			ps.setString(2, pwd);//向第二个占位符中放值
			rs=ps.executeQuery();
			if(rs.next()) {//如果有值，说明数据库中肯定存在此用户
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return false;
	}
	
	//接着改造 有时候，我不知道要传几个参数怎么办？
	//方法的参数可以这么定义   可以传一个数组
	//这么写，代码会更通用
	public boolean login3(String sql,Object[] params) {
		getConn();
		//? 就是占位符
		//String sql="select * from login where username=? and pwd=?";
		try {
			//创建ps对象，并且预编译sql语句
			ps=conn.prepareStatement(sql);
			//向sql语句中放值
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i+1, params[i]);
			}
			rs=ps.executeQuery();
			if(rs.next()) {//如果有值，说明数据库中肯定存在此用户
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return false;
	}
	
	public Connection  getConn2() {
		Connection conn=null;
		try {
			Context cxt=new InitialContext();
			DataSource ds=(DataSource)cxt.lookup("java:comp/env/jdbc/news");
			conn = ds.getConnection();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	//关闭资源
	public void closeAll() {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(st !=null) {
				st.close();
			}
			if(conn !=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	

}
